<!doctype html>
<!--
@license
Copyright (c) 2015 The Polymer Project Authors. All rights reserved.
This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt
The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt
The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt
Code distributed by Google as part of the polymer project is also
subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt
-->
<html>
<head>
  <meta charset="utf-8">

  <script src="../../../webcomponentsjs/webcomponents-lite.js"></script>
  <script src="../../../web-component-tester/browser.js"></script>

  <link rel="import" href="../../polymer.html">
  <link rel="import" href="gestures-elements.html">

</head>
<body>

  <script>

    suite('simulate events', function() {

      var app;

      setup(function() {
        app = document.createElement('x-app');
        document.body.appendChild(app);
      });

      teardown(function() {
        document.body.removeChild(app);
      });

      test('tap on x-foo and check localTarget and rootTarget', function() {
        var foo = app.$.foo;
        foo.dispatchEvent(new CustomEvent('click'));
        assert.equal(app._testLocalTarget, foo);
        assert.equal(app._testRootTarget, foo);
      });

      test('tap on x-foo.div and check localTarget and rootTarget', function() {
        var foo = app.$.foo;
        var div = foo.$.div;
        div.dispatchEvent(new CustomEvent('click'));
        assert.equal(app._testLocalTarget, foo);
        assert.equal(app._testRootTarget, div);
        assert.equal(foo._testLocalTarget, div);
        assert.equal(foo._testRootTarget, div);
      });

    });

    suite('Event Setup and Teardown', function() {
      var outer, inner;
      suiteSetup(function() {
        outer = document.createElement('x-setup');
        inner = outer.$.inner;
      });

      suite('setup', function() {
        test('listeners block', function() {
          assert.equal(outer.__polymerGesturesTouchAction, 'none');
          var obj = outer.__polymerGestures;
          assert.equal(obj.mousedown.downup, 2, 'mousedown downup');
          assert.equal(obj.mousedown.track, 1, 'mousedown track');
          assert.equal(obj.mousedown.tap, 1, 'mousedown tap');
          assert.equal(obj.touchstart.downup, 2, 'touchstart downup');
          assert.equal(obj.touchmove.track, 1, 'touchmove track');
          assert.equal(obj.touchstart.tap, 1, 'touchstart tap');
          assert.equal(obj.touchend.downup, 2, 'touchend downup');
          assert.equal(obj.touchend.track, 1, 'touchend track');
          assert.equal(obj.touchend.tap, 1, 'touchend tap');
        });

        test('on-*', function() {
          assert.equal(inner.__polymerGesturesTouchAction, 'none');
          var obj = inner.__polymerGestures;
          assert.equal(obj.mousedown.downup, 2, 'mousedown downup');
          assert.equal(obj.mousedown.track, 1, 'mousedown track');
          assert.equal(obj.mousedown.tap, 1, 'mousedown tap');
          assert.equal(obj.touchstart.downup, 2, 'touchstart downup');
          assert.equal(obj.touchmove.track, 1, 'touchmove track');
          assert.equal(obj.touchstart.tap, 1, 'touchstart tap');
          assert.equal(obj.touchend.downup, 2, 'touchend downup');
          assert.equal(obj.touchend.track, 1, 'touchend track');
          assert.equal(obj.touchend.tap, 1, 'touchend tap');
        });

        test('dynamic', function() {
          var el = document.createElement('x-dynamic');
          var obj = el.__polymerGestures;
          assert(!obj);
          el.setup();
          obj = el.__polymerGestures;
          assert(obj, 'gestures object exists');
          assert.equal(obj.mousedown.tap, 1, 'mousedown tap');
          assert.equal(obj.click.tap, 1, 'click tap');
          assert.equal(obj.touchstart.tap, 1, 'touchstart tap');
          assert.equal(obj.touchend.tap, 1, 'touchend tap');
        });
      });

      suite('teardown', function() {
        test('listeners block', function(){
          var events = Object.keys(outer.listeners);
          events.forEach(function(ev) {
            outer.unlisten(outer, ev, outer.listeners[ev]);
          });
          assert.equal(outer.__polymerGesturesTouchAction, 'none');
          var obj = outer.__polymerGestures;
          assert.equal(obj.mousedown.downup, 0, 'mousedown downup');
          assert.equal(obj.mousedown.track, 0, 'mousedown track');
          assert.equal(obj.mousedown.tap, 0, 'mousedown tap');
          assert.equal(obj.touchstart.downup, 0, 'touchstart downup');
          assert.equal(obj.touchmove.track, 0, 'touchmove track');
          assert.equal(obj.touchstart.tap, 0, 'touchstart tap');
          assert.equal(obj.touchend.downup, 0, 'touchend downup');
          assert.equal(obj.touchend.track, 0, 'touchend track');
          assert.equal(obj.touchend.tap, 0, 'touchend tap');
        });

        test('on-*', function(){
          var innerNotes = outer._notes[0];
          innerNotes.events.forEach(function(no) {
            outer.unlisten(inner, no.name, no.value);
          });
          assert.equal(inner.__polymerGesturesTouchAction, 'none');
          var obj = inner.__polymerGestures;
          assert.equal(obj.mousedown.downup, 0, 'mousedown downup');
          assert.equal(obj.mousedown.track, 0, 'mousedown track');
          assert.equal(obj.mousedown.tap, 0, 'mousedown tap');
          assert.equal(obj.touchstart.downup, 0, 'touchstart downup');
          assert.equal(obj.touchmove.track, 0, 'touchmove track');
          assert.equal(obj.touchstart.tap, 0, 'touchstart tap');
          assert.equal(obj.touchend.downup, 0, 'touchend downup');
          assert.equal(obj.touchend.track, 0, 'touchend track');
          assert.equal(obj.touchend.tap, 0, 'touchend tap');
        });

        test('dynamic', function(){
          var el = document.createElement('x-dynamic');
          var obj = el.__polymerGestures;
          assert(!obj);
          el.setup();
          el.teardown();
          obj = el.__polymerGestures;
          assert(obj, 'gestures object exists');
          assert.equal(obj.mousedown.tap, 0, 'mousedown tap');
          assert.equal(obj.click.tap, 0, 'click tap');
          assert.equal(obj.touchstart.tap, 0, 'touchstart tap');
          assert.equal(obj.touchend.tap, 0, 'touchend tap');
        });
      });
    });

    suite('target finding', function() {
      var div, divLocation;

      setup(function() {
        div = document.createElement('div');
        div.style.cssText = 'height: 50px; width: 50px; background: red;';
        div.id = 'target';
        document.body.appendChild(div);
        divLocation = div.getBoundingClientRect();
      });

      test('target finding returns null outside the window', function() {
        var actual = Polymer.Gestures.deepTargetFind(-1, -1);
        assert.equal(actual, null);
      })

      test('find the div in document', function() {
        var x = divLocation.left, y = divLocation.top;
        var actual = Polymer.Gestures.deepTargetFind(x, y);
        assert.equal(actual, div);
      });

      test('find the div with a shadowroot', function() {
        if (!div.createShadowRoot) {
          return;
        }
        div.createShadowRoot();
        var x = divLocation.left, y = divLocation.top;
        var actual = Polymer.Gestures.deepTargetFind(x, y);
        assert.equal(actual, div);
      });

      test('find the div inside a shadowroot', function() {
        if (!div.createShadowRoot) {
          return;
        }
        var divOwner = document.createElement('span');
        document.body.appendChild(divOwner);
        divOwner.createShadowRoot().appendChild(div);
        var bcr = divOwner.getBoundingClientRect();
        var x = bcr.left, y = bcr.top;
        var actual = Polymer.Gestures.deepTargetFind(x, y);
        assert.equal(actual, div);
      });

      test('find the div with a shadowroot inside a shadowroot', function() {
        if (!div.createShadowRoot) {
          return;
        }
        div.createShadowRoot();
        var divOwner = document.createElement('span');
        document.body.appendChild(divOwner);
        divOwner.createShadowRoot().appendChild(div);
        var bcr = divOwner.getBoundingClientRect();
        var x = bcr.left, y = bcr.top;
        var actual = Polymer.Gestures.deepTargetFind(x, y);
        assert.equal(actual, div);
      });
    });

    // TODO(dfreedm): Add more gesture tests!

  </script>

</body>
</html>
